package com.mj.activity.user;

import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.mj.MJApplaction;
import com.mj.R;
import com.mj.activity.MainActivity;
import com.mj.base.BaseActivity;
import com.mj.bean.UserInfo;
import com.mj.http.Json;
import com.mj.http.Url;

public class Activity_Sign extends BaseActivity {
	private String mobile, vcode, pwds;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_sign);
	}

	private boolean checkMobile() {
		mobile = aq.id(R.id.editText1).getText().toString();
		if (TextUtils.isEmpty(mobile)) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_LONG).show();
			aq.id(R.id.editText1).getView().requestFocus();
			return false;
		}
		return true;
	}

	private boolean checkAll() {
		if (!checkMobile()) {
			return false;
		}
		vcode = aq.id(R.id.editText2).getText().toString();
		if (TextUtils.isEmpty(vcode)) {
			Toast.makeText(this, "请输入验证码", Toast.LENGTH_LONG).show();
			aq.id(R.id.editText2).getView().requestFocus();
			return false;
		}
		pwds = aq.id(R.id.editText3).getText().toString();
		if (TextUtils.isEmpty(pwds)) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
			aq.id(R.id.editText3).getView().requestFocus();
			return false;
		}
		String repwds = aq.id(R.id.editText4).getText().toString();
		if (TextUtils.isEmpty(repwds)) {
			Toast.makeText(this, "请确认密码", Toast.LENGTH_LONG).show();
			aq.id(R.id.editText4).getView().requestFocus();
			return false;
		}
		if (!repwds.equals(pwds)) {
			Toast.makeText(this, "密码与确认密码不符", Toast.LENGTH_LONG).show();
			aq.id(R.id.editText3).getView().requestFocus();
			return false;
		}
		return true;
	}

	public void getVcode(View view) {
		if (checkMobile()) {
			baseCallBack callBack = new baseCallBack() {
				public void onSuccess(org.json.JSONObject object) {
					super.onSuccess(object);
					Toast.makeText(Activity_Sign.this, "发送成功,请查收", Toast.LENGTH_LONG).show();
					aq.id(R.id.editText2).text("1234");
				};
			};
			String url = Url.vcode + "?mobile=" + mobile;
			getJson(url, callBack, false);
		}
	}

	public void commit(View view) {
		if (checkAll()) {
			baseCallBack callBack = new baseCallBack() {
				public void onSuccess(org.json.JSONObject object) {
					super.onSuccess(object);
					saveUser(object);
					showBuilder();
				};
			};
			String url = Url.regist + "?mobile=" + mobile + "&vcode=" + vcode + "&pwds=" + pwds;
			getJson(url, callBack, false);

		}
	}

	private void showBuilder() {
		Builder builder = new Builder(Activity_Sign.this);
		builder.setTitle("注册成功");
		builder.setItems(new String[] { "完善个人信息", "随便看看" }, new OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				Intent intent = new Intent();
				switch (arg1) {
				case 0:
					intent.setClass(Activity_Sign.this, Activity_Info.class);
					break;
				case 1:
					intent.setClass(Activity_Sign.this, MainActivity.class);
					break;
				}
				startActivity(intent);
				finish();
			}
		});
		builder.setCancelable(false);
		builder.show();
	}

	private void saveUser(JSONObject object) {
		String token = Json.getToken(object);
		String userkey = Json.getUserkey(object);
		SharedPreferences preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString("token", token);
		editor.putString("userkey", userkey);
		editor.commit();
		MJApplaction.userInfo = new UserInfo();
		MJApplaction.userInfo.setToken(token);
		MJApplaction.userInfo.setUserkey(userkey);
	}
}
